<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<script>
    //对象转查询字符串
    function objToSearchString(obj){
        let str = ''
        for( let key in obj ){
            str += `${key} = ${obj[key]}&`
        }
        return str.slice( 0 , str.length-1 )
    }
    //封装ajax函数
    function ajax( method , url , params , cb ){
        //1、创建请求对象
        let xhr = new XMLHttpRequest()

        //2、配置请求对象
        //参数一：请求方法
        //参数二：请求地址
        xhr.open( method , url+"?" + objToSearchString(params) )

        //3、设置回调函数
        xhr.onreadystatechange = function(){
            //判断
            if( xhr.readyState == 4 ){
                cb( JSON.parse(xhr.responseText) )
            }
        }

        //4、发送请求
        xhr.send()
    }
</script>